package com.imc.api.order.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imc.api.order.dao.IOrderMapper;
import com.imc.api.order.repository.IApiLessonMapper;
import com.imc.core.config.feign.MyPageImpl;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 类描述：API课程服务类
 * 开发者：YT
 * 创建日期：2018/10/28 21:37
 */
@Service
@Slf4j
@Transactional
public class ApiLessonService {

    @Autowired
    IApiLessonMapper iApiLessonMapper;//API课程mapper

    @Autowired
    IOrderMapper iOrderMapper;//订单mapper

    /*
     * YT  2018/10/28 21:38
     * 方法描述：查询推荐的课程列表
     */
    public List <Map <String, Object>> getRecommendLessonList(Integer sizeVideo, Integer sizeCast) {
        List <Map <String, Object>> lessonList = iApiLessonMapper.getRecommend(sizeVideo, sizeCast);
        List <Map <String, Object>> l = new ArrayList <>();
        if (lessonList == null || lessonList.isEmpty()) {
            return l;
        }
        Map <String, Object> videoList = new HashMap <>();
        videoList.put("title", "热门舞蹈视频");
        videoList.put("type", "video");
        Map <String, Object> castList = new HashMap <>();
        castList.put("title", "热门课程");
        castList.put("type", "goods");

        List <Map <String, Object>> childVideo = new ArrayList <>();
        List <Map <String, Object>> childCast = new ArrayList <>();

        for (Map <String, Object> map1 : lessonList) {
            Map <String, Object> m_ = new HashMap <>();
            m_.put("image", map1.get("photo"));
            m_.put("label", map1.get("lessonName"));
            m_.put("id", map1.get("id"));
            if (map1.get("lessonType").equals("1")) {
                childCast.add(m_);
            } else if (map1.get("lessonType").equals("2")){
                childVideo.add(m_);
            }
        }

        videoList.put("child", childVideo);
        castList.put("child", childCast);

        l.add(castList);
        l.add(videoList);

        return l;
    }

    /*
     * YT  2018/10/28 21:40
     * 方法描述：获取视频列表  假分页
     */
    public MyPageImpl getVideo(Pageable pageable) {
        PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        val data = iOrderMapper.queryVideoList();
        PageInfo info = new PageInfo <>(data);
        return new MyPageImpl(info.getList(), info.isIsLastPage());
    }
}
